<?php

class Damages_Methods_Request_New_CarDamage extends Damages_Methods_Request {

  /**
   *
   * @var Damages_Methods_Data_CarDamage_New 
   */
  public $carDamage;

  public function __construct($params) {
    parent::__construct($params);
    $this->carDamage = new Damages_Methods_Data_CarDamage_New($params->carDamage);
  }

  public function process() {
    // TODO: walidacja po akcji i procesie

    if (!$this->carDamage->validate())
      throw new Damages_Exception_App(Damages_Codes::INVALID_NEW_DAMAGE);
    $carData = DamagesCarDataQuery::create()->findOneById($this->carDamage->carData->id);
    $con = Propel::getConnection(Damages_Config::getInstance()->datasource);
    try {
      $con->beginTransaction();
      $carDamage = new DamagesCarDamages();
      $carDamage->setProcessTypeId($this->carDamage->process->id);
      $carDamage->setCarDataId($this->carDamage->carData->id);
      $carDamage->setStatusId($this->carDamage->status->id);
      $carDamage->setDictPerpetratorId($this->carDamage->perpetrator->id);
      $carDamage->setDamagesDescription($this->carDamage->damageDescription);
      $carDamage->setDescription($this->carDamage->description);
      $carDamage->setDamageDate($this->carDamage->damageDate);
      if ($this->carDamage->ol && $this->carDamage->ol->id)
        $carDamage->setOlId($this->carDamage->ol->id);
      foreach ($this->carDamage->damages as $damage) {
        $dbDamage = new DamagesDamages();
        $dbDamage->setIsMain((int) $damage->isMain);
        $dbDamage->setDictDamageScaleId($damage->scale->id);
        $dbDamage->setDictDamageSpotId($damage->spot->id);
        $dbDamage->setDictDamageTypeId($damage->type->id);
        $carDamage->addDamages($dbDamage);
      }
      foreach ($this->carDamage->estimatedBudgets as $key => $estimatedBudget) {
        $attachment = DamagesAttachmentsQuery::create()->findOneById($estimatedBudget->attachment->id);
        $targetDir = Damages_Config::getInstance()->uploads->mainPath . $carData->getVin() . '/' . Damages_Config::getInstance()->uploads->subdirs->estimatedCosts;
        $targetFileName = sprintf(Damages_Config::getInstance()->uploads->patterns->estimatedCosts, $key);
        $attachment = $this->processAttachment($attachment, $targetDir, $targetFileName);
        $dbBudget = new DamagesEstimatedBudgets();
        $dbBudget->setValue($estimatedBudget->value);
        $dbBudget->setAttachments($attachment);
        $carDamage->addEstimatedBudgets($dbBudget);
      }
      if ($this->carDamage->attachments) {
        if ($this->carDamage->attachments->photos)
          foreach ($this->carDamage->attachments->photos as $key => $photo) {
            $attachment = DamagesAttachmentsQuery::create()->findOneById($photo->id);
            $targetDir = Damages_Config::getInstance()->uploads->mainPath . $carData->getVin() . '/' . Damages_Config::getInstance()->uploads->subdirs->photos;
            $targetFileName = sprintf(Damages_Config::getInstance()->uploads->patterns->photos, $key);
            $attachment = $this->processAttachment($attachment, $targetDir, $targetFileName);
            $carDamage->addAttachments($attachment);
          }
        if ($this->carDamage->attachments->additional)
          foreach ($this->carDamage->attachments->additional as $key => $additional) {
            $attachment = DamagesAttachmentsQuery::create()->findOneById($additional->id);
            $targetDir = Damages_Config::getInstance()->uploads->mainPath . $carData->getVin() . '/' . Damages_Config::getInstance()->uploads->subdirs->additional;
            $targetFileName = sprintf(Damages_Config::getInstance()->uploads->patterns->additional, $key);
            $attachment = $this->processAttachment($attachment, $targetDir, $targetFileName);
            $carDamage->addAttachments($attachment);
          }
        // protocol
        if ($this->carDamage->attachments->protocol && $this->carDamage->attachments->protocol->id) {
          $attachment = DamagesAttachmentsQuery::create()->findOneById($this->carDamage->attachments->protocol->id);
          $targetDir = Damages_Config::getInstance()->uploads->mainPath . $carData->getVin() . '/' . Damages_Config::getInstance()->uploads->subdirs->protocol;
          $targetFileName = sprintf(Damages_Config::getInstance()->uploads->patterns->protocol);
          $attachment = $this->processAttachment($attachment, $targetDir, $targetFileName);
          $carDamage->addAttachments($attachment);
        }

        // waybill
        if ($this->carDamage->attachments->waybill && $this->carDamage->attachments->waybill->id) {
          $attachment = DamagesAttachmentsQuery::create()->findOneById($this->carDamage->attachments->waybill->id);
          $targetDir = Damages_Config::getInstance()->uploads->mainPath . $carData->getVin() . '/' . Damages_Config::getInstance()->uploads->subdirs->waybill;
          $targetFileName = sprintf(Damages_Config::getInstance()->uploads->patterns->waybill);
          $attachment = $this->processAttachment($attachment, $targetDir, $targetFileName);
          $carDamage->addAttachments($attachment);
        }
      }
      $carDamage->save();
      $con->commit();
    } catch (Exception $exc) {
      $con->rollBack();
      throw $exc;
    }
    $response = new Damages_Methods_Response(true);
    return $response;
  }

  private function processAttachment(DamagesAttachments $attachment, $targetDir, $targetFileName) {
    if (!is_dir(APPLICATION_PATH . '/../public' . $targetDir))
      mkdir(APPLICATION_PATH . '/../public' . $targetDir, 0777, true);
    $sourcePath = APPLICATION_PATH . '/../public' . $attachment->getFilePath();
    if (!is_file($sourcePath))
      throw new Damages_Exception_App(Damages_Codes::NO_ATTACHMENT_FOUND);
    $sourceFname = $attachment->getName();
    $newFname = $targetFileName . Core_Tools::getFileExtension($sourceFname);
    $newPath = $targetDir . $newFname;
    $fullPath = APPLICATION_PATH . '/../public' . $newPath;
    if (rename($sourcePath, $fullPath)) {
      $attachment->setTemp(0);
      $attachment->setName($newFname);
      $attachment->setFilePath($newPath);
    }

    return $attachment;
  }

}